﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/3 16:01:39
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using Mozlite.Common.Configuration;
    using System.Web.Mvc;

    /// <summary>
    /// 应用程序配置基类。
    /// </summary>
    public class ConfigurationBase : BaseObject
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.ConfigurationBase"/>。
        /// </summary>
        public ConfigurationBase() { }

        /// <summary>
        /// 获取扩展数据列。
        /// </summary>
        protected override string ExtendFieldName
        {
            get
            {
                return "ConfigurationData";
            }
        }

        private int settingsID = -1;
        /// <summary>
        /// 获取或设置网站设置ID。
        /// </summary>
        public int SettingsID { get { if (settingsID == -1) settingsID = Settings.SettingsID; return settingsID; } set { settingsID = value; } }

        /// <summary>
        /// 获取或设置应用程序GUID。
        /// </summary>
        public string ApplicationID { get; set; }

        /// <summary>
        /// 最后更新时间。
        /// </summary>
        public DateTime LastModify { get; set; }

        /// <summary>
        /// 获取或设置默认模板。
        /// </summary>
        public string DefaultTheme { get { return Get<string>("Theme", Settings.Theme); } set { Set("Theme", value); } }

        /// <summary>
        /// 获取模板名称。
        /// </summary>
        /// <param name="context">控制器上下文。</param>
        /// <returns>返回当前模板名称。</returns>
        protected internal virtual string GetTheme(ControllerContext context)
        {
            return this.DefaultTheme;
        }

        /// <summary>
        /// 从数据库中加载数据。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        protected override void Load(IDataReader reader)
        {
            this.SettingsID = reader.GetInt32("SettingsID");
            this.ApplicationID = reader.GetString("ApplicationID");
            this.LastModify = reader.GetDateTime("LastModify");
        }

        private SiteSettings settings;
        /// <summary>
        /// 当前配置程序。
        /// </summary>
        protected SiteSettings Settings
        {
            get
            {
                if (settings == null)
                    settings = SiteSettings.Current;
                return settings;
            }
        }

        /// <summary>
        /// 获取或设置当前区域的域名。
        /// </summary>
        public string Domain { get { return Get<string>("Domain"); } set { Set("Domain", value); } }

        /// <summary>
        /// 默认语言。
        /// </summary>
        public string Language { get { return Get<string>("Language", Settings.Language); } set { Set("Language", value); } }

        /// <summary>
        /// 从数据库中创建实例对象。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        /// <returns>返回当前配置实例。</returns>
        public static ConfigurationBase CreateInstance(IDataReader reader) {
            var type = Type.GetType(reader.GetString("type"));
            if (type == null)
                return null;
            var instance = Activator.CreateInstance(type) as ConfigurationBase;
            if (instance != null)
                instance.LoadFromDataReader(reader);
            return instance;
        }
    }

    /// <summary>
    /// 应用程序配置基类。
    /// </summary>
    /// <typeparam name="TConfiguration">应用程序配置类。</typeparam>
    public abstract class ConfigurationBase<TConfiguration> : ConfigurationBase
        where TConfiguration : ConfigurationBase<TConfiguration>, new()
    {
        /// <summary>
        /// 获取当前配置实例对象。
        /// </summary>
        /// <param name="applicationID">应用程序ID。</param>
        /// <returns>返回当前应用程序实例。</returns>
        public static TConfiguration GetConfiguration(string applicationID) {
            var configuration = Applications.GetConfiguration(applicationID, SiteSettings.Current.SettingsID) as TConfiguration;
            if (configuration == null)
            {
                configuration = new TConfiguration();
            }
            return configuration;
        }
    }
}
